<?php

namespace App\Http\Controllers\Info;

use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Notifications\DatabaseNotification;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('infor.user.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\User $user
     * @return \Illuminate\Http\Response
     */
    public function show(User $user)
    {
//        $aa  = $user->favorite;
//        dd($aa);
//        foreach($aa as $v){
//            dump($v->belongsModel->toArray());
//        }
        return view('home.user.show',compact('user'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\User $user
     * @return \Illuminate\Http\Response
     */
    public function edit(User $user, Request $request)
    {
        //
        $this->authorize('update',$user);
        $name = $request->type;
        return view('infor.user.edit_' . $name, compact('user'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \App\User $user
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, User $user)
    {
        $this->authorize('update',$user);
        $request->validate([
            //sometimes,当提交的数据中有某个字段时候才对进行验证
            'password'=>'sometimes|min:6|required|confirmed',
            'name'=>'sometimes|required'
        ],[
            'password.min'=>'密码不能少于6位',
            'password.required'=>'密码不能为空',
            'password.confirmed'=>'两次密码输入不一致',
            'name'=>'昵称不能为空'
        ]);
        $data = $request->all();
        if (isset($data['password'])) {
            $data['password'] = bcrypt($data['password']);
        }
        $user->update($data);
        return back()->with('success', '修改成功');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\User $user
     * @return \Illuminate\Http\Response
     */
    public function destroy(User $user)
    {
        //
    }

    //关注 取消关注  利用  toggle属性 如果数据库有就移除 没有就添加
    public function ToggleFollower(User $user){
//        dd($user->toArray());
        //自己不能关注自己
        $this->authorize('isSub',$user);
        //通过用户模型找到 follower方法 去数据库里查询是否存在
        auth()->user()->follower()->toggle($user->id);
        return back()->with('success','操作成功');
    }

    public function ReadNotify(DatabaseNotification $notification){
        $notification->markAsRead();
//        dd($notification->toArray());
        return redirect ($notification['data']['link']);

    }


}
